package com.lyons.generate.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DBUtils {

	public static String dataBaseName;
	public static String username;
	public static String password;

	private static Connection connection;

	public static Connection getConnection() {

		if (null == connection) {

			String url = "jdbc:mysql://127.0.0.1:3306/" + dataBaseName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
			try {

				Class.forName("com.mysql.cj.jdbc.Driver");
				connection = DriverManager.getConnection(url, username, password);
			} catch (ClassNotFoundException | SQLException e) {
				log.error("数据库链接失败...{}", e.getMessage());
			}
		}
		return connection;
	}

	public static void colse(ResultSet rs, PreparedStatement ps, Connection connection) {

		try {

			if (null != rs) {
				rs.close();
			}
			if (null != ps) {
				ps.close();
			}
			if (null != connection) {
				connection.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
